from rest_framework import serializers
from .models import *
from management.models import PositionModel
import re
from django.forms import ValidationError
from management.models import RoleModel


# 员工的序列化器
class StaffSer(serializers.ModelSerializer):
    class Meta:
        password = serializers.CharField(max_length=256, write_only=True, trim_whitespace=True)
        model = StaffModel
        fields = [
            "id",
            "name",
            "account",
            "mobile",
            "birthday",
            "region",
            "position_id",
            "role_id",
        ]

    # 重写验证方法，添加自己的验证规则
    def validate(self, attrs):
        name = attrs.get("name")
        account = attrs.get("account")
        mobile = attrs.get("mobile")
        password = attrs.get("password")
        position_id = attrs.get("position_id")
        role_id = attrs.get("role_id")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{1,9}$", name):
            raise ValidationError(f'{name} 姓名由2到10位汉字、数字、字母或下划线组成！')

        if not re.findall(r"^[\w]{1,9}$", account):
            raise ValidationError(f'{account} 账号由2到10位数字、字母或下划线组成！')

        if not re.findall(r"^1[3-9]\d{9}$", mobile):
            raise ValidationError(f'{mobile} 手机号格式不正确！')

        if not re.findall(r"^[\w\W]{6,256}$", f"{password}"):
            raise ValidationError(f'密码由6到20位数字、字母或下划线组成！')

        position_list = PositionModel.objects.filter(is_delete=False).values("id")
        position_list = [i.get("id") for i in position_list]
        if position_id not in position_list:
            raise ValidationError(f"{position_id} 该职位不存在！")

        role_list = RoleModel.objects.filter(is_delete=False).values("id")
        role_list = [i.get("id") for i in role_list]
        if role_id not in role_list:
            raise ValidationError(f"{role_id} 该角色不存在！")

        return attrs


# 邮件表的序列化器
class EmailSer(serializers.ModelSerializer):
    class Meta:
        model = EmailModel
        fields = [
            "id",
            "title",
            "content",
            "create_date"
        ]
    # 重写验证方法，添加自己的验证规则
    def validate(self, attrs):
        title = attrs.get("title")
        content = attrs.get("content")

        if not re.findall(r"^[\u4e00-\u9fa5\w\W]{2,128}$",title):
            raise ValidationError(f'{title} 标题由2到128位汉字、数字、字母或下划线组成！')

        if not re.findall(r"^[\u4e00-\u9fa5\w\W]{2,512}$",content):
            raise ValidationError(f'{content} 内容允许2到512位汉字、数字、字母或下划线组成！')

        return attrs


# 收发邮件表的序列化器
class SendToEmailSer(serializers.ModelSerializer):
    class Meta:
        model = SendToEmailModel
        fields = [
            "id",
            "send_staff_id",
            "to_staff_id",
            "email_id",
            "create_date",
        ]

    # 重写验证方法，添加自己的验证规则
    def validate(self, attrs):
        send_staff_id = attrs.get("send_staff_id")
        to_staff_id = attrs.get("to_staff_id")

        staff_list = StaffModel.objects.filter(is_delete=False).values("id")
        staff_list = [i.get("id") for i in staff_list]
        if send_staff_id != None:
            if send_staff_id not in staff_list:
                raise ValidationError(f"{send_staff_id} 该发件人不存在！")

        if to_staff_id != None:
            if to_staff_id not in staff_list:
                raise ValidationError(f"{to_staff_id} 该收件人不存在！")

        return attrs


# 邮件附件的序列化器
class EmailAppendixSer(serializers.ModelSerializer):
    class Meta:
        model = EmailAppendixModel
        fields = [
            "id",
            "email_id",
            "appendix_url",
            "create_date",
        ]

    # 重写验证方法，添加自己的验证规则
    def validate(self, attrs):
        appendix_url = attrs.get("appendix_url")

        if not re.findall(r"^[\u4e00-\u9fa5\w\W]{1,256}$", appendix_url):
            raise ValidationError(f'{appendix_url} 附件路径允许2到256位汉字、数字、字母或下划线组成！')

        return attrs


# 材料序列化器
class MaterialsSer(serializers.ModelSerializer):
    class Meta:
        model = MaterialsModel
        fields = [
            "id",
            "name",
            "url",
            "create_date",
        ]

    def validate(self, attrs):
        name = attrs.get("name")
        url = attrs.get("url")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$",name):
            raise ValidationError(f'{name} 名称由2到30位汉字、数字、字母或下划线组成！')

        return attrs


# 工资序列化器
class PayrollSer(serializers.ModelSerializer):
    class Meta:
        model = PayrollModel
        fields = [
            "id",
            "staff_id",
            "attendance",
            "absenteeism",
            "leave",
            "base_salary",
            "position_salary",
        ]

    def validate(self, attrs):
        staff_id = attrs.get("staff_id")
        attendance = attrs.get("attendance")
        absenteeism = attrs.get("absenteeism")
        leave = attrs.get("leave")
        base_salary = attrs.get("base_salary")
        position_salary = attrs.get("position_salary")

        staff_list = StaffModel.objects.filter(is_delete=False).values("id")
        staff_list = [i.get("id") for i in staff_list]
        if staff_id not in staff_list:
            raise ValidationError(f"{staff_id} 该员工不存在！")

        if not re.findall(r"\d$",f"{attendance}"):
            raise ValidationError(f'{attendance} 出勤天数由数字组成！')
        if not re.findall(r"\d$",f"{absenteeism}"):
            raise ValidationError(f'{absenteeism} 旷工时间由数字组成！')
        if not re.findall(r"\d$",f"{leave}"):
            raise ValidationError(f'{leave} 请假天数由数字组成！')
        if not re.findall(r"\d+\.?\d*$",f"{base_salary}"):
            raise ValidationError(f'{base_salary} 基本工资由 浮点类型 组成！')
        if not re.findall(r"\d+\.?\d*$",f"{position_salary}"):
            raise ValidationError(f'{position_salary} 岗位工资由 浮点类型 组成！')

        return attrs
